/*
 * Copyright (c) 2014 - 2017. Company.Domain Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.activity.delivery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiji.www.frameworks.utils.DensityUtils;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.BaseActivity;
import cn.com.dhc.danlu.dlware.OperatorUtils;
import cn.com.dhc.danlu.dlware.net.GeneralBusinessObserver;
import cn.com.hd.mall.web.webservices.entity.request.order.OrderLogRequest;
import cn.com.hd.mall.web.webservices.entity.response.order.OrderLogResponse;
import cn.com.hd.mall.web.webservices.entity.response.order.OrderLogResponse.OrderLog;

import static cn.com.dhc.danlu.R.id.tv_order_status;
import static cn.com.dhc.danlu.R.id.tv_time;
import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * author: 吴海洋(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * author: 刘立琦(<a href="mailto:liuliqi@danlu.com">liuliqi@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-01-17 10:32<br/>
 * <p>
 * <p>
 * 订单跟踪界面
 * </p>
 */
public class OrderTrackActivity extends BaseActivity {

    private String mOrderNo;

    /***
     * 服务器请求交互实现类
     */
    // private DanluTaskProxy danluTaskProxy;

    /**
     * 显示订单号的 展示
     */
    private TextView tvOrderNum;

    /**
     * 订单跟踪消息容器
     */
    private LinearLayout llOrderLogContainer;

    public static void startSelf(Context context, String orderNo) {
        Intent intent = new Intent(context, OrderTrackActivity.class);
        intent.putExtra("orderNo", orderNo);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getDelegate ().setContentView (R.layout.layout_order_track, R.layout_order_track.class);
        setContentView(R.layout.activity_order_track);

        tvOrderNum = (TextView) findViewById(R.id.tv_order_num);
        llOrderLogContainer = (LinearLayout) findViewById(R.id.ll_order_log_container);
        mOrderNo = getIntent().getStringExtra("orderNo");
        // low的方式特殊处理了 kiss 原则
        findViewById(R.id.ll_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!TextUtils.isEmpty(mOrderNo)) {
            getOrderTrackLog();
            // 刷新头部显示的订单消息
            tvOrderNum.setText("以下为订单" + mOrderNo + "的跟踪消息");
        }
    }

    @Override
    protected String getActivityTitle() {
        return null;
    }

    private void getOrderTrackLog() {
        OrderLogRequest request = new OrderLogRequest();
        request.setToken(agent.getToken());
        request.setOrderNo(mOrderNo);
        agent.getApi()
                .getOrderTrackLog(request)
                .compose(OperatorUtils.<OrderLogResponse>defaultSchedulers())
                .subscribe(new GeneralBusinessObserver<OrderLogResponse>(this, R.string.loading_common_msg) {
                    @Override
                    protected void onSuccess(OrderLogResponse res) {
                        handleSuccess(res);
                    }

                    @Override
                    protected void onAlertTips(String displayMessage) {
                        handleErrorStatus(displayMessage);
                    }
                });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        //todo 获取 推送传过来的orderNo 然后重新刷新界面
    }

    protected void handleSuccess(OrderLogResponse response) {
        List<OrderLog> logList = response.getOrderLogList();
        if (!logList.isEmpty()) {
            llOrderLogContainer.removeAllViews();
            for (int i = 0; i < logList.size(); i++) {
                OrderLog orderLog = logList.get(i);
                View itemView = View.inflate(this, R.layout.item_order_track, null);
                TextView tvTime = (TextView) itemView.findViewById(tv_time);
                TextView tvOrderStatus = (TextView) itemView.findViewById(tv_order_status);
                tvTime.setText(orderLog.getDealDate());
                tvOrderStatus.setText(orderLog.getDealDescrip());
                llOrderLogContainer.addView(itemView);
            }
        } else {
            handleErrorStatus(getString(R.string.no_order_track));
        }
    }

    protected void handleErrorStatus(String res) {
        llOrderLogContainer.removeAllViews();
        TextView textView = new TextView(this);
        textView.setText(res);
        textView.setTextSize(DensityUtils.dip2px(this, 15));
        llOrderLogContainer.addView(textView);
    }
}